#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, const char *argv[])
{
    DIR *dir = NULL;
    struct dirent *dent;
    struct stat st = {0};
    char path[100] = "./music/";
    char newname[100] = {0};
    dir = opendir(path);
    if (dir == NULL)
    {
        perror("opendir");
        exit(-1);
    }
    dent = readdir(dir);

    while (dent != NULL)
    {
        if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0)
        {
            dent = readdir(dir);
            continue;
        }
        memset(newname, 0, 100);
        strcpy(newname, path);
        strcat(newname, dent->d_name);

        if (stat(newname, &st) == -1)
        {
            perror("stat");
            exit(-1);
        }
        if (S_ISDIR(st.st_mode))
        {
            if (dir == NULL)
            {
                perror("opendir");
                return 0;
            }

            struct dirent *dent;
            int ct = 0;

            while ((dent = readdir(dir)) != NULL)
            {
                ct++;
                if (ct == 2)
                {
                   rmdir(newname);
                }
        
        else if (S_ISREG(st.st_mode) && st.st_size == 0)
        {
            remove(newname);
        }
        }
        }
        dent = readdir(dir);
    }
    closedir(dir);
    return 0;
}
/*int empty(const char *path) {
    DIR *dir = opendir(path);
    if (dir == NULL) {
        perror("opendir");
        return 0;
    }

    struct dirent *dent;
    int ct = 0;
    while ((dent = readdir(dir)) != NULL) {
        ct++;
        if (ct > 2) {
            break;
        }
    }

    closedir(dir);
    return ct == 2;
}*/
